package pl.com.hme.mediaplayer.activities;


import pl.com.hme.mediaplayer.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

/**
 * @author DB
 * 
 */
public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnPreferenceChangeListener {

	private static final String TAG = "TaxPreferenceActivity";
	private ListPreference mStartTime;
	private ListPreference mStopTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Load the preferences from an XML resource
		addPreferencesFromResource(R.xml.preferences);
		
	    mStartTime = (ListPreference) getPreferenceScreen().findPreference("start_time");
	    mStopTime = (ListPreference) getPreferenceScreen().findPreference("stop_time");

	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		Preference pref = findPreference(key);
		if (pref instanceof ListPreference) {
			ListPreference listPref = (ListPreference) pref;
			pref.setSummary(listPref.getEntry());
		}
	}


	@Override
	protected void onResume() {
		super.onResume();
		mStartTime.setSummary(mStartTime.getEntry());
		mStopTime.setSummary(mStopTime.getEntry());
		
		
	    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

	}
	
	@Override
	protected void onPause() {
		super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); 
	}

	public boolean onPreferenceChange(Preference pref, Object newValue) {
		if (pref instanceof ListPreference) {
			ListPreference listPref = (ListPreference) pref;
			pref.setSummary(listPref.getEntry());
		}
		return true;
	}

}
